package com.sihai.ehr.service.impl;

import com.sihai.core.*;
import com.sihai.core.support.HibernateDataAccessor;
import com.sihai.ehr.model.biz.ExamQuanItemSet;
import com.sihai.ehr.model.biz.ExamQuanSet;
import com.sihai.ehr.service.ExamItemSetService;
import com.sihai.ehr.web.pojo.ExamItemSetPojo;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("examItemSetService")
public class ExamItemSetServiceImpl implements ExamItemSetService {

    @Resource
    private DataAccessor dataAccessor;

    @Override
    public LimitedList<ExamItemSetPojo> findByTopOrganization(Long organizationId, Limit limit, Sorter sorter) {
        String hql = "select new com.sihai.ehr.web.pojo.ExamItemSetPojo(d,e.id,e.pState,e.itemCount) from ExamQuanSet e right join e.duty d where (d.organization.id=? or d.organization.topOrganization.id=?) and d.cancel <> 1";
        //String hql = "select new com.sihai.ehr.web.pojo.ExamItemSetPojo(d,e.id,e.percentageQuantitative,q.itemCount) from ExamBaseConfig e right join e.duty d left join ExamQuanSet q where d.id=q.duty.id and d.organization.id=? or d.organization.topOrganization.id=?";
        return dataAccessor.findByHql(ExamItemSetPojo.class, hql, new Object[]{organizationId, organizationId}, limit, sorter);
    }

    @Override
    public LimitedList<ExamItemSetPojo> findByOrganization(Long organizationId, Limit limit, Sorter sorter) {
        String hql = "select new com.sihai.ehr.web.pojo.ExamItemSetPojo(d,e.id,e.pState,e.itemCount) from ExamQuanSet e right join e.duty d where d.organization.id=?";
        return dataAccessor.findByHql(ExamItemSetPojo.class, hql, new Object[]{organizationId}, limit, sorter);
    }

    @Override
    public ExamQuanSet getExamQuanSet(Long id) {
        return dataAccessor.get(ExamQuanSet.class, id);
    }

    @Override
    public LimitedList<ExamQuanItemSet> findByExamQuanSet(Long id, Limit limit, Sorter sorter) {
        return dataAccessor.findByHql(ExamQuanItemSet.class, "from ExamQuanItemSet e where e.examQuanSet.id=?", new Object[]{id}, limit, sorter);
    }

    @Override
    public void saveExamQuanSet(ExamQuanSet set) {
        if (set != null)
            dataAccessor.save(set);
    }

    @Override
    public void saveExamItem(ExamQuanItemSet item) {
        if (item != null)
            dataAccessor.saveOrUpdate(item);

    }

    @Override
    public ExamQuanItemSet findExamQuanItemSetById(Long id) {
        return dataAccessor.load(ExamQuanItemSet.class, id);
    }

    @Override
    public ExamQuanSet findExamQuanSetByDutyId(final Long dutyId) {
        return dataAccessor.findUnique(ExamQuanSet.class,new QueryCreator(){
            @Override
            public Query createQuery(Session session) {
                Query query=session.createQuery("from ExamQuanSet e where e.duty.id=:dutyId");
                query.setParameter("dutyId",dutyId);
                return query;
            }
        });
    }

    @Override
    public void removeQuanItem(final String itemIds,Long quanSetId) {
        if (itemIds == null || itemIds.isEmpty()) {
            return;
        }                      
//        //得到待删除的考核项目总比例     exam_quan_item_set
//        Double itemSum=dataAccessor.findUnique(Double.class,new QueryCreator(){
//            @Override
//            public Query createQuery(Session session) {
//                return session.createQuery(String.format("select sum(e.percentage) from ExamQuanItemSet e where e.id in (%s)",itemIds));
//            }
//        });

        //删除定量考核项目
        dataAccessor.batchUpdate(new QueryCreator() {
            @Override
            public Query createQuery(Session session) {

                return session.createQuery(String
                        .format("delete ExamQuanItemSet e where e.id in (%s)",
                        itemIds));

            }
        });
        
        //更新考核项目设定
        ExamQuanSet set=getExamQuanSet(quanSetId);
        //set.setpState(set.getpState()-itemSum.floatValue());
        set.setItemCount(set.getItemCount()-itemIds.split(",").length);
        dataAccessor.update(set);
    }

    @Override
    public Float getItemSumByQuanSetId(final Long quanSetId) {
                //得到待删除的考核项目总比例     exam_quan_item_set
        Double itemSum=dataAccessor.findUnique(Double.class,new QueryCreator(){
            @Override
            public Query createQuery(Session session) {
                return session.createQuery(String.format("select sum(e.percentage) from ExamQuanItemSet e where e.examQuanSet.id=%d",quanSetId));
            }
        });
        if(itemSum!=null)
            return itemSum.floatValue();
        else
            return 0f;
    }
}
